﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.ServiceProcess;

namespace Daneas.Utility.Universals
{
    public class IISHelper
    {
        /// <summary>
        /// 获取应用程序池->数组
        /// </summary>
        /// <returns></returns>
        public TResult<List<AppPool>> GetAppPools()
        {
            TResult<List<AppPool>> TR = new TResult<List<AppPool>>();

            try
            {

                DirectoryEntry directoryEntry = new DirectoryEntry("IIS://LOCALHOST/W3SVC/AppPools", ConfigHelper.GetConfigString("iisuser"), ConfigHelper.GetConfigString("iispassword"));
                if (directoryEntry == null)
                {
                    TR.Data = null;
                    TR.isSuccess = true;
                    TR.Msg = "木有找到程序池";
                    return TR;
                }

                List<AppPool> list = new List<AppPool>();

                foreach (DirectoryEntry entry in directoryEntry.Children)
                {
                    PropertyCollection properties = entry.Properties;
                    AppPool pool = new AppPool();
                    pool.Name = entry.Name;

                    //ServiceController sc = new ServiceController();
                    //sc.ServiceName = entry.Name;
                    //pool.State = sc.Status.ToString();

                    list.Add(pool);
                }

                TR.Data = list;
                TR.isSuccess = true;
                return TR;
            }
            catch (Exception ex)
            {
                TR.Msg = ex.Message;
                TR.isSuccess = false;
                return TR;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="AppPoolName">应用程序池命名，当找不到该应用程序池时，系统会报找不到指定路径</param>
        /// <param name="method">启动命令:"Start"， 停止：“Stop” ； 回收：“Recycle”</param>
        /// <returns></returns>
        public Result AppPoolOper(string AppPoolName, string method)
        {
            Result re = new Result();

            try
            {
                DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools", ConfigHelper.GetConfigString("iisuser"), ConfigHelper.GetConfigString("iispassword"));

                DirectoryEntry findPool = appPool.Children.Find(AppPoolName, "IIsApplicationPool");

                findPool.Invoke(method, null);

                appPool.CommitChanges();

                appPool.Close();

                re.isSuccess = true;
            }

            catch (Exception ex)
            {
                re.isSuccess = false;
                re.Msg = ex.Message;
            }

            return re;
        }
    }

    /// <summary>
    /// 应用程序池
    /// </summary>
    public class AppPool
    {
        /// <summary>
        /// 程序池名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 程序池状态
        /// </summary>
        public string State { get; set; }

        /// <summary>
        /// 程序池下的网站
        /// </summary>
        public List<string> Sites { get; set; }
    }

}
